package com.xzx.study;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.xzx.study.bean.ShopInfo;

import java.util.ArrayList;
import java.util.List;

public class ListViewBaseAdapterActivity extends AppCompatActivity {

    private static final String TAG = ListViewBaseAdapterActivity.class.getSimpleName();
    private ListView listView3;

    private List<ShopInfo> data = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_base_adapter);

        listView3 = findViewById(R.id.listView3);

        ShopInfo shopInfo = new ShopInfo(R.drawable.f1, "name---1", "content---1");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f2, "name---2", "content---2");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f3, "name---3", "content---3");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f4, "name---4", "content---4");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f5, "name---5", "content---5");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f6, "name---6", "content---6");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f7, "name---7", "content---7");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f8, "name---8", "content---8");
        data.add(shopInfo);
        shopInfo = new ShopInfo(R.drawable.f9, "name---9", "content---9");
        data.add(shopInfo);


        listView3.setAdapter(mAdapter);
    }

    private BaseAdapter mAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.i(TAG, "===convertView=====" + convertView);
            if (convertView == null) {
                convertView = View.inflate(ListViewBaseAdapterActivity.this, R.layout.item_list_view_simple_adapter,
                                         null);
            }
            ShopInfo shopInfo = data.get(position);
            ImageView icon = convertView.findViewById(R.id.simple_adapter_icon);
            TextView name = convertView.findViewById(R.id.simple_adapter_name);
            TextView content = convertView.findViewById(R.id.simple_adapter_content);
            icon.setImageResource(shopInfo.getIcon());
            name.setText(shopInfo.getName());
            content.setText(shopInfo.getContent());

            return convertView;
        }
    };
}
